package com.demo.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;

public class FtpTest {
	
	 public static void main(String[] args) {       
	        //testUpload();       
	        //testDownload();       
	    }       
	     
	    /**     
	     * FTP上传单个文件测试     
	     */       
	    public static void testUpload() {       
	        FTPClient ftpClient = new FTPClient();       
	        FileInputStream fis = null;       
	     
	        try {       
	            ftpClient.connect("主机ip");       
	            ftpClient.login("root", "root");       
	            File srcFile = new File("F:/tempfiles/2_201083103325.png");       
	            fis = new FileInputStream(srcFile);       
	            //设置上传目录       
	            ftpClient.changeWorkingDirectory("/u01");       
	            ftpClient.setBufferSize(1024);       
	            ftpClient.setControlEncoding("UTF-8");       
	            //设置文件类型（二进制）       
	            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);       
	            ftpClient.storeFile("1234.png", fis);       
	            System.out.println("成功！");      
	        } catch (IOException e) {       
	            e.printStackTrace();       
	            throw new RuntimeException("FTP客户端出错！", e);       
	        } finally {       
	            IOUtils.closeQuietly(fis);       
	            try {       
	                ftpClient.disconnect();       
	            } catch (IOException e) {       
	                e.printStackTrace();       
	                throw new RuntimeException("关闭FTP连接发生异常！", e);       
	            }       
	        }       
	    }       
	     
	    /**     
	     * FTP下载单个文件测试     
	     */       
	    public static boolean OneFileDownload(String remoteFileName,String target,String newname) {
	    	//System.out.println(remoteFileName);
	        FTPClient ftpClient = new FTPClient();       
	        FileOutputStream fos = null;       
	        Boolean bool=false;
	        try {       
	            //ftpClient.connect();
	            ftpClient.connect("192.168.3.237", 21);
	            ftpClient.login("senkenftp", "000000");
	            fos = new FileOutputStream(target+newname);
	            ftpClient.setBufferSize(1024);       
	            //设置文件类型（二进制）       
	            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);   
	            ftpClient.enterLocalPassiveMode();
	            bool = ftpClient.retrieveFile(remoteFileName, fos);       
	        } catch (IOException e) {       
	            e.printStackTrace();       
	            throw new RuntimeException("FTP Client ERROR！", e);
	        } finally {       
	            IOUtils.closeQuietly(fos);       
	            try {       
	                ftpClient.disconnect();       
	            } catch (IOException e) {       
	                e.printStackTrace();       
	                throw new RuntimeException("CLOSE FTP ERROR！", e);       
	            }       
	        }
	        return bool;
	    }  
	    

}
